package game;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import entities.NPC;
import entities.Player;
import environment.Grass;

public class GameState extends BasicGameState {
	
	Input input;
	
	Grass grass;
	Player player;
	NPC npc;
	Map map;
	Message message;
	
	Image imageGrass;
	Image imagePlayer;
	Image imageNPC;
	
	Animation playerAnim;
	
	float xOff;
	float yOff;
	
	boolean isShop;
	
	static ArrayList<NPC> entities = new ArrayList<NPC>();
	
	public int getID() {
		return Main.GAMESTATE;
	}

	public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
		playerAnim = getAnimation(0, 0, 3, 0, 250);
		player = new Player(0, 0, playerAnim);
		
		imageNPC = new Image("assets/img/player.png");
		npc = new NPC((int) 13.75f, (int) 3.75f, imageNPC);
		entities.add(npc);
		
		imageGrass = new Image("assets/img/grass.png");
		grass = new Grass(imageGrass);
		
		//TODO Fix dialogues majorly!
		message = new Message(player.x, player.y, "What would you like to buy? ");
		 
		try {
			map = new Map(63, 63, new File("assets/levels/tarhur.txt"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		input = gc.getInput();
	}

	public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException {
		g.translate(-(int) xOff * 32, -(int) yOff * 32);
		
		grass.draw(g);
		map.draw(g);
		player.animationDraw(g);
		
		npc.draw(g);
		
		if (isShop) {
			message.draw(g);
		}
		
		g.translate(xOff * 32, yOff * 32);
	}

	public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {			
		playerAnim.update(delta);
		
		if (input.isKeyPressed(Input.KEY_LEFT)) {
			move(-1, 0);
		} else if (input.isKeyPressed(Input.KEY_RIGHT)) {
			move(1, 0);
		} else if (input.isKeyPressed(Input.KEY_UP)) {
			move(0, -1);
		} else if (input.isKeyPressed(Input.KEY_DOWN)) {
			move(0, 1);
		}
		
		if (isShop && input.isKeyPressed(Input.KEY_ESCAPE)) {
			isShop = false;
		}
		
		xOff = player.getX() - 7;
		yOff = player.getY() - 4;
		
		if (xOff < 0) xOff = 0;
		if (yOff < 0) yOff = 0;
		if (xOff > 47.5f) xOff = 47.5f;
		if (yOff > 52.5f) yOff = 52.5f;
	}
	
	public void move(int dx, int dy) {
		player.checkX = player.x + dx;
		player.checkY = player.y + dy;
		
		if (player.checkX < 0) return;
        if (player.checkY < 0) return;
        if (player.checkX > 61.5f) return;
        if (player.checkY > 61.5f) return;
		
        if (!map.collide(player.checkX, player.checkY) && !map.isNPC(player.checkX, player.checkY) && !isShop) {
	    	player.x += dx;
	    	player.y += dy;
		} 
	    
	    if (map.isNPC(player.checkX, player.checkY)) {
	    	isShop = true;
	    }
	    
	    if (map.isDoor(player.checkX, player.checkY)) {
	    	map.metalDoor = map.metalDoorOpen;
	    }
	}
	
	public Animation getAnimation(int x1, int y1, int x2, int y2, int duration) throws SlickException {
		Image pss = new Image("assets/img/pss.png");
		SpriteSheet sheet = new SpriteSheet(pss, 32, 32);
		return new Animation(sheet, x1, y1, x2, y2, true, duration, false);
	}
}
